package com.tensquare.filter;

import com.netflix.zuul.ZuulFilter;
import com.netflix.zuul.context.RequestContext;
import com.netflix.zuul.exception.ZuulException;
import org.apache.commons.lang3.StringUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Component;
import util.JwtUtil;
import javax.servlet.http.HttpServletRequest;

/**
 * @author Macross
 * @Type WebFilter.java
 * @Desc 前端网关过滤器
 * @date 2019/12/27 14:31
 */
@Component
public class WebFilter extends ZuulFilter {

    @Autowired
    private JwtUtil jwtUtil;

    @Override
    public String filterType() {
        return "pre";
    }

    @Override
    public int filterOrder() {
        return 0;
    }

    @Override
    public boolean shouldFilter() {
        return true;
    }

    @Override
    public Object run() throws ZuulException {
        //获取请求上下文对象
        RequestContext currentContext = RequestContext.getCurrentContext();
        //获取请求对象request
        HttpServletRequest request = currentContext.getRequest();
        //获取请求头
        String header = request.getHeader("Authorization");
        //判断请求头是否为空
        if (StringUtils.isNotBlank(header)) {
            //将请求头放入网关请求头中
            currentContext.addZuulRequestHeader("Authorization",header);
        }
        //放行
        return null;
    }
}
